import getData from '../getData/api.js'
import {constantRouterMap} from '../router.js'
// import {constantRouterMap} from '../router.js'
import router from '../router'
import { Message } from 'element-ui';
const state = {
    roles: [], //用户类别
    routers: [], //动态路由列表
    params:{},
}

const getters = {

}

const mutations = {
    SET_ROLES(state, data) {
        state.roles = data
    },
    SET_ROUTES(state, data) {
        // console.log(data)
        state.routers = data;
    },

}

const actions = {

    // generateRoutes({state, commit}, params) {
    //     return getData.getHomeData(params)
    //         .then((res) => {
    //             return commit('SET_ROUTES', res);
    //         })
    //         .catch((err)=>{
    //                console.log(err)
    //         })
    // },

    getRoutes({state, commit}, params) {
        return getData.getRoutesData(params)
            .then((res) => {
                state.params=params
                if (res.status==200){
                    actions.getmenu(params);

                }

                // } else {
                //     Message.error(res.errorData)
                // }
                return commit('SET_ROUTES', res.data);

            })
            .catch((err)=>{
                // console.log(err)
            })
    },
    getmenu({state, commit}, params) {
        return getData.findUserResources(params)
            .then((res) => {
                if (res.status==200){
                    console.log(state.params)
                    localStorage.setItem('role1',state.params.get('username'))
                    for (let i = 0; i<res.data.length; i++) {
                        if (res.data[i].nodes==null){
                            res.data[i].nodes=[];
                        }
                    }
                    let btnPermission=[];
                    let menuArr=res.data;
                    Object.keys(menuArr).forEach((key) => {
                        if (menuArr[key].type=="button"){
                            btnPermission.push(this.menuArr[key].permission)
                        }
                        if (menuArr[key].nodes!=null){
                            Object.keys(menuArr[key].nodes).forEach((key1) => {
                                if (menuArr[key].nodes[key1].type=="button"){
                                    btnPermission.push(menuArr[key].nodes[key1].permission)
                                }
                                if (menuArr[key].nodes[key1].nodes!=null){
                                    Object.keys(menuArr[key].nodes[key1].nodes).forEach((key2) => {
                                        if (menuArr[key].nodes[key1].nodes[key2].type=="button"){
                                            btnPermission.push(menuArr[key].nodes[key1].nodes[key2].permission)
                                        }
                                    });
                                }
                            });
                        }
                    });
                    localStorage.setItem('btnPermission', JSON.stringify(btnPermission));
                    localStorage.setItem('resourcesrList', JSON.stringify(res.data));//后台返回的权限
                    if (JSON.parse(localStorage.getItem('resourcesrList')) != null){
                        let routers1=JSON.parse(localStorage.getItem('resourcesrList'));
                            router.push({
                                path: routers1[0].url,
                            });

                    }
                }

            })
    },
    generateRoles({state,commit}, params) {
        return new Promise(resolve => {
            commit('SET_ROLES', params);
            resolve()
        })

    },
    generateRoutes({state,commit}, params) {
        commit('SET_ROUTES', constantRouterMap);

    }
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}
